<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Mail;
use Session;
use Hash;
class peopleController extends Controller
{       
   
    public function getPeople()
    {  
       // echo 12312;
          //存入客户信息
        // session(['user_id'=>15]);
        // session(['user_email'=>'postmaster@feimail.cn']);
        // session(['user_username'=>'xiaofeifei']);
        $id = session('user_id');
        // echo $id;
        //zheye_user
        $username = DB::table('zheye_user')->where('id',$id)->value('username');
        //zheye_user_details
        $res =  DB::table('zheye_user_details')->where('uid',$id)->first();
       
        // var_dump($res);    
        return view('/home/people',['row'=>$res,'username'=>$username]);
        
    }

      public function getInbox()
    {   
       

      
        return view('/home/inbox');
        
    }

    public function getSetting()
    {   
        
      
       
        $id = session('user_id');
       

        $ress = DB::table('zheye_user')->where('id',$id)->first();
        $res =  DB::table('zheye_user_details')->where('uid',$id)->first();
        return view('/home/setting',['rows'=>$ress,'row'=>$res]);
    }

    public function getMenu()
    {
        return view('/home/menu');
    }
    /**
    * 个人中心资料修改页面 进入处理方法 
    */

    public function getPeoedit()
    {   
        // echo 123;
       $id = session('user_id');
       $username = session('user_username');
        // $num = 100;
        // echo $id;

        $res =  DB::table('zheye_user_details')->where('uid',$id)->first();
        // dd($res);
        $res->username = $username;  
       //判断信息完成度
        $headimg = $res->headimg;
        $sex = $res->sex;
        $trade = $res->trade;
        $address = $res->address;
        $company = $res->company;
        $school = $res->school;
        $synopsis = $res->synopsis;
        $num = 0;
        
        $num += !empty($headimg) ? 20 : 0;
        $num += !empty($sex) ? 15 : 0;
        $num += !empty($trade) ? 15 : 0;
        $num += !empty($address) ? 15 : 0;
        $num += !empty($company) ? 15 : 0;
        $num += !empty($school) ? 10 : 0;
        $num += !empty($synopsis) ? 10 : 0;

        return view('/home/peoedit',['row'=>$res,'num'=>$num]);

      
        // return view('/home/peoedit');
        
    }

    /**
    *  个人中心信息修改处理方法
    */
    public function postDoform(Request $request)
    {       
        
        //id测试写的
        
        //接受数据
        $id = session('user_id');

        $res = $request->except('_token');
        $file =  $request->only('photo');
        $username = DB::table('zheye_user')->where('id',$id)->value("username");
        //随机名
        // $name = md5(rand(10000,99999).time());
        
        if($request->hasFile('photo')){
        //获取后缀名
        $suffix = $request->file('photo')->getClientOriginalExtension();

        $request->file('photo')->move('./image/head/',$username.'.'.$suffix);

        $res['headimg'] = '/image/head/'.$username.'.'.$suffix;
        //销毁缓存图片
        unset($res['photo']);
        }
        //修改数据
        $a = DB::table('zheye_user_details')->where('uid',$id)->update($res);
        if($a or $request->hasFile('photo') ){
            return redirect('/people/success')->with('success','修改成功');
            // return view('/system/success')->with('success','修改成功');//x  view不能存session
        }
            //只有back()->with() 和 redirect()->with() 可以存session
            return back()->with('error','尚未修改,禁止提交!!!');

    }

     /**
     * 个人中心信息修改成功跳转(/people/peoedit )
     */
      
       public function  getSuccess()
        {
            return view('/system/success');
        }
        public function getSettings()
        {

           
            $id = session('user_id');
            $ress = DB::table('zheye_user')->where('id',$id)->first();
            return view('/home/settings',['ress'=>$ress]);
        }
        /**
        * 个人中心找回密码发送验证处理方法
        */
        public function getSendCode()//Requrest $requrest
        {  
            $id = session('user_id');
            $email = session('user_email');
            $usernam = session('user_username');
            $arr = [$id,$email,$usernam];
            // echo $email;
            $a = rand(100000,999999);
             session(['vcode'=>$a]);
            // Session::flash(['vcode'=>$a]);
            //发送验证码到邮箱 $mail 返回int 收件人有n个 返回int(n);
            $mail =  Mail::raw($a,function($m) use ($arr){
                        $m->from('zheye@lnmp.space','zheye官网邮件');
                        $m->to($arr[1],$arr[2])->subject('请接收验证码');
                        });
            if($mail){
                echo '验证码已发送到你的邮箱请注意查收';
            }else{
                echo '验证码发送失败';

            }

        }

        public function getCeshi(Request $request)
        {   
             session(['vcode'=>123456]);
             $vcode = $request->only('vcode');
             // var_dump($vcode['vcode']);
             // var_dump(session('vcode'));die;
             if( $vcode['vcode'] != session('vcode')){
                return '验证错误';
             }else{
               return '验证码正确';
             }
            
        }

        /**
        * 检测用户填写的验证码是否正确
        */
        public function getTestVcode(Request $request)
        {   
             // session(['vcode'=>"123456"]);
             $postTestVcode = $request->only('vcode');
             if($postTestVcode['vcode'] != session('vcode')){
             return 0;

            // return back()->with('error','验证码有误');
             }
             return redirect('/people/xgmm')->with('success','密码修改成功请重新登陆');
              // echo json_encode($postTestVcode);
               // echo '验证码正确';
        }
        /**
        * 修改密码跳转页面
        */
        public function getXgmm()
        {
            // return view('/system/xgmm');
            $id = session('user_id');
            $res = DB::table('zheye_user')->where('id',$id)->first();
            // dd($res);
            return view('/home/xgmm',['ress'=>$res]);
        }

        public function postUpdatemm(Request $request)
        {
            $password = $request->password;
            $password = Hash::make($password);


            // echo $password;
            $res = DB::table('zheye_user')->where('id',session('user_id'))->update(["password"=>$password]);//返回受影响的行数,如果原密码和修改的一样返回0 不过这里加密了,所以密码的字符串不会一样,所以说只要修改了就会返回1(这里就修改了一个所以是1)
            // dd($res);
            if($res == 0){
                return 0;
            }
               return  redirect('/people/relogin')->with('success','密码修改成功,请使用新密码重新登陆');
        }
        public function getRelogin()
        {
            return view('/system/xgmm');
        }



}
